#include "light_sensor.h"
#include "my_struct.h"
#include "encode.h"
//左右的零点误差
#if 0
#define  ZERO_ADVALUE  -8
//左右的阈值
#define  HIGH_SENSOR_VALUE  3500
#define  LOW_SENSOR_VALUE   300
//STOP_FLAG标志位置
#define  stop           1
#define  cicrle         2
#define  go             0
//中间异常阈值
#define     exception_middle_value   400
//左右阈值
#define     LEFT_THERSH 2000
#define     RIGHT_THERSH 2000
//判定完全偏离轨道的阈值
#define     Leave_value  1000
//左右最大值
#define     LEFT_MAX  2000
#define     RIGHT_MAX 2000
unsigned char flag;
unsigned char stop_flag;
#else


#endif

//获取循迹传感器输出函数
//返回值：int类型，范围循迹传感器数据，根据此值来调节小车舵机角度
int Light_Sensor_GetErrorValue(light_values* tracing)
{
	int error;//定义数据输出变量
    int Mid_AD = tracing->mid_light;//获取中间传感器的数据
    int Left_AD = tracing->left_light;//获取左边传感器的数据
    int Right_AD = tracing->right_light;//获取右边传感器的数据
	error=(Left_AD-Right_AD+D_AD_VALUE);
  //偏左
	if((error<0)&&(Mid_AD<LEFT_THERSH))
	{	
		error=LEFT_MAX_ERROR+ (LEFT_MAX_ERROR-error);
	} 
	else if((error>0)&&(Mid_AD<RIGHT_THERSH))
	{
		error=2*RIGHT_MAX_ERROR-error;
	}
	return error;
}


/*************************************注释
short Light_Sensor_GetErrorValue(tracing_Str* tracing)
{
    
    int error = 0;
    //这种状况就停下来，或者缓慢原地打转，异常情况；
    if(tracing->left_tracing > HIGH_SENSOR_VALUE&&tracing->right_tracing > HIGH_SENSOR_VALUE&&tracing->mid_tracing > HIGH_SENSOR_VALUE)
    {   
        stop_flag = stop;
    }
    else//正常行驶
    {
        stop_flag = go;
    }
    error = tracing->left_tracing - tracing->right_tracing + ZERO_ADVALUE;
    //正常行驶,在轨迹上
    if(tracing->mid_tracing > exception_middle_value)
    {
        flag = 0;
    }
    if((tracing->mid_tracing <= LEFT_THERSH) && (error < -20)) //小车偏左
    { //left
      error= (2*LEFT_MAX-error);
      flag--;
    } 
    else if((tracing->mid_tracing  <= RIGHT_THERSH) && (error > 10))//小车偏右
    { //right
      error= (2*RIGHT_MAX-error);
      flag++;
    }
    if(tracing->left_tracing < Leave_value && tracing->mid_tracing  < Leave_value && tracing->right_tracing < Leave_value)
    {
          if(flag<0)
       {//left
          error = LEFT_MAX*2;
       }
       else
       {//right
          error = RIGHT_MAX*2;
       }
  }  
    return error;
}
*/